﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[AddComponentMenu("Tween/Tween Position")]
public class TweenPositionAxis : UITweener
{
    public enum Axis
    {
        Axis_X = 0,
        Axis_Y = 1,
        Axis_Z = 2,

    }

    public Axis axis = Axis.Axis_X;

    public float from;
    public float to;

    //public Vector3 from;
    //public Vector3 to;

    //[HideInInspector]
    //public bool worldSpace = true;

    Transform mTrans;

    public Transform cachedTransform { get { if (mTrans == null) mTrans = transform; return mTrans; } }


    /// <summary>
    /// Tween's current value.
    /// </summary>

    //public Vector3 value
    //{
    //    get
    //    {
    //        //return worldSpace ? cachedTransform.position : cachedTransform.localPosition;
    //        return cachedTransform.localPosition;
    //    }
    //    set
    //    {
    //        //if (worldSpace)
    //        //{
    //        //	if (worldSpace) 
    //        //                 cachedTransform.position = value;
    //        //	else
    //        //                 cachedTransform.localPosition = value;
    //        //}	
    //        cachedTransform.localPosition = value;
    //    }
    //}

    public float value
    {
        get
        {
            switch (axis)
            {
                case Axis.Axis_X:
                    return cachedTransform.localPosition.x;
                case Axis.Axis_Y:
                    return cachedTransform.localPosition.y;
                case Axis.Axis_Z:
                    return cachedTransform.localPosition.z;
            }
            return cachedTransform.localPosition.x;
        }
        set
        {
            switch (axis)
            {
                case Axis.Axis_X:
                    cachedTransform.localPosition = new Vector3(value, cachedTransform.localPosition.y, cachedTransform.localPosition.z);
                    break;
                case Axis.Axis_Y:
                    cachedTransform.localPosition = new Vector3(cachedTransform.localPosition.x, value, cachedTransform.localPosition.z);
                    break;
                case Axis.Axis_Z:
                    cachedTransform.localPosition = new Vector3(cachedTransform.localPosition.x, cachedTransform.localPosition.y, value);
                    break;
            }
        }
    
    }


    /// <summary>
    /// Tween the value.
    /// </summary>

    protected override void OnUpdate(float factor, bool isFinished) { value = from * (1f - factor) + to * factor; }

    /// <summary>
    /// Start the tweening operation.
    /// </summary>

    static public TweenPosition Begin(GameObject go, float duration, Vector3 pos)
    {
        TweenPosition comp = UITweener.Begin<TweenPosition>(go, duration);
        comp.from = comp.value;
        comp.to = pos;

        if (duration <= 0f)
        {
            comp.Sample(1f, true);
            comp.enabled = false;
        }
        return comp;
    }

    ///// <summary>
    ///// Start the tweening operation.
    ///// </summary>

    //static public TweenPosition Begin (GameObject go, float duration, Vector3 pos)
    //{
    //	TweenPosition comp = UITweener.Begin<TweenPosition>(go, duration);
    //	//comp.worldSpace = worldSpace;
    //	comp.from = comp.value;
    //	comp.to = pos;

    //	if (duration <= 0f)
    //	{
    //		comp.Sample(1f, true);
    //		comp.enabled = false;
    //	}
    //	return comp;
    //}

    [ContextMenu("Set 'From' to current value")]
    public override void SetStartToCurrentValue() { from = value; }

    [ContextMenu("Set 'To' to current value")]
    public override void SetEndToCurrentValue() { to = value; }

    [ContextMenu("Assume value of 'From'")]
    void SetCurrentValueToStart() { value = from; }

    [ContextMenu("Assume value of 'To'")]
    void SetCurrentValueToEnd() { value = to; }
}
